#pragma once
#include "Comm.hpp"
#include <iostream>
class Init//管理管道的创建和销毁
{
public:
    Init()
    {
        umask(0);//把权限掩码置为0
        int n = ::mkfifo(gpipefile.c_str(), gmode);//创建管道文件，指定路径，指定文件权限
        if (n < 0)
        {
            std::cerr << "mkfifo error" << std::endl;
            return;
        }
        std::cout << "mkfifo success" << std::endl;
    }
    ~Init()
    {
        // 删除管道
        int n = ::unlink(gpipefile.c_str());//直接根据路径删除管道
        if (n < 0)
        {
            std::cerr << "unlink error" << std::endl;
            return;
        }
        std::cout << "unlink success" << std::endl;
    }
};

Init init;//单例模式

class Server
{
public:
    Server() : _fd(gdefault)
    {
    }

    bool OpenPipeForRead()
    {
       _fd = OpenPipe(gForRead);
       if(_fd < 0)return false;
       return true;
    }
    int RecvPipe(std::string *out)//输出型参数
    {
        char buffer[gsize];
        size_t n = read(_fd, buffer, sizeof(buffer));//把管道里的内容读到字符串数组中
        if(n > 0)
        {
            buffer[n] = 0;
            *out = buffer;
        }
        return n;
    }
    void ClosePipe()
    {
        ClosePipeHelper(_fd);
    }

private:
    int _fd;
};